package com.bw.liuyushan.mykotlin_firstmvptest.MVP.Fragment

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.bw.liuyushan.mvp_kotlin_lib.View.BaseFragment
import com.bw.liuyushan.mykotlin_firstmvptest.MVP.Contrack.IContrackTest
import com.bw.liuyushan.mykotlin_firstmvptest.MVP.Entity.Data
import com.bw.liuyushan.mykotlin_firstmvptest.MVP.Entity.Food
import com.bw.liuyushan.mykotlin_firstmvptest.MVP.Model.BaseModelTest
import com.bw.liuyushan.mykotlin_firstmvptest.MVP.Presenter.BasePresenterTest

import com.bw.liuyushan.mykotlin_firstmvptest.R
import com.bw.liuyushan.mykotlin_firstmvptest.Adapter.FoodAdapter
import com.bw.liuyushan.mykotlin_firstmvptest.Adapter.FoodAdapter2
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_blank.*

/**
 * 展示 刷新 加载
 */
class BlankFragment : BaseFragment<BasePresenterTest>(),IContrackTest.C_ViewTest,OnRefreshLoadMoreListener {
    var page : Int=1//定义页数变量
    var isAdapter : Boolean=false
    var foodAdapte:FoodAdapter2 ?=null//?可以为空
    override fun onLoadMore(refreshLayout: RefreshLayout) {
        isAdapter=false
        page++
        ip.mpDate("/ios/cf/dish_list.php?stage_id=1&limit=20&page="+page)

    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isAdapter=true
        page=1
        ip.mpDate("/ios/cf/dish_list.php?stage_id=1&limit=20&page="+page)
    }

    override fun bindLayout(): Int {
        return R.layout.fragment_blank
    }

    override fun initView() {
        //连接!!
        ip = BasePresenterTest(this,BaseModelTest())
        //瀑布流 布局管理器
        rv_food.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)
        sm_food.setOnRefreshLoadMoreListener(this)//刷新加载
    }



    override fun initDate() {
        ip.mpDate("/ios/cf/dish_list.php?stage_id=1&limit=20&page="+page)

    }



    override fun ShowDate(t: Food) {
        super.ShowDate(t)
        var data: MutableList<Data> = t.data

        initAdapter(data)
    }

    private fun initAdapter(data: MutableList<Data>) {
        sm_food.finishLoadMore()//结束加载
        sm_food.finishRefresh()//结束刷新
        if (foodAdapte==null){
            foodAdapte = FoodAdapter2(R.layout.rv_food_layout,data)
            rv_food.adapter = foodAdapte
        }else{//!!非空判断 不等于空
            if (isAdapter){
                foodAdapte!!.data.clear()
            }
            foodAdapte!!.data.addAll(data)
            foodAdapte!!.notifyDataSetChanged()
        }
    }


}


